const {spawn} = require('child_process');
const path = require('path')

module.exports = {
  runBat: (payload) => {
    const {
      ue4Map = 'Grasslands',
      originPosX = 0,
      originPosY = 0,
      originYaw = 0,
      uavType = 0,
      taskInfo = ''
    } = payload

    const runCmd = uavType == 0 ?
      ['/c', 'SITLRun.bat', ue4Map, originPosX, originPosY, originYaw, taskInfo]
      : ['/c', 'SITLRunFW.bat', ue4Map, originPosX, originPosY, originYaw, taskInfo]
    // 获取当前 .exe 文件所在的目录路径
    const exeFilePath = process.platform === 'win32' ? process.execPath : '';
    const exeDir = path.dirname(exeFilePath);
    const bat = spawn(
      'cmd.exe',
      runCmd,
      {
      // cwd: 'C:\\PX4PSP\\RflySimAPIs'
      cwd: process.env.NODE_ENV === 'development' ? path.resolve(__dirname, '../../resources/bat') : `${exeDir}/resources/bat`
    });

    bat.stdout.on('data', (data) => {
      console.log(`stdout: ${data}`);
    });

    bat.stderr.on('data', (data) => {
      console.error(`stderr: ${data}`);
    });

    bat.on('exit', (code) => {
      console.log(`子进程退出码: ${code}`);
    });
    return bat;
    //
    // // 自动输入
    // bat.stdin.write('1\n');
  }
}

